//
//  CCForumPushViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCForumPushViewController.h"

@interface CCForumPushViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation CCForumPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatTableViewVC];
    self.navigationController.navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 64)];
    [headView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [self.view addSubview:headView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:backButton];
    [backButton setFrame:CGRectMake(mainScreen_Width / 22.058, mainScree_Height / 24.703, mainScreen_Width / 12.5, mainScree_Height / 22.233)];
    [backButton setTitleColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000] forState:UIControlStateSelected];
    _collectionArray = [NSMutableArray array];
    //    _titleArary = [[NSMutableArray alloc]init];
    _pushArray = [[NSMutableArray alloc]init];
    self.page = 0;
    self.isLoding = NO;
    [self addHeader];
    [self releaseFoot];
    
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.tabBarController.tabBar.hidden = YES;
}
#pragma mark - 下拉加载
- (void)addHeader
{
    __block CCForumPushViewController *vc = self;
    [self.collectionPushTableView addLegendHeaderWithRefreshingBlock:^{
        
        
        vc.isLoding = NO;
        [vc AFDate:0 loding:NO];
    }];
    [self.collectionPushTableView.header beginRefreshing];
}
#pragma mark - 上拉加载
- (void)releaseFoot
{
    __block CCForumPushViewController *vc = self;
    [self.collectionPushTableView addLegendFooterWithRefreshingBlock:^{
        vc.page ++;
        vc.isLoding = YES;
        [vc AFDate:vc.page loding:YES];
    }];
}
#pragma mark - 解析数据
-(void)AFDate:(NSInteger)number loding:(BOOL)isLoding
{
    NSString *moderatorsStr = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getForumInfo.php?forumId=%@&ver=6.2.2",self.forumId];
    [AFNblock getdataWithString:moderatorsStr block:^(id block) {
        
        [_titleArary removeAllObjects];
        _titleArary = [[NSMutableArray alloc]init];
        
        
        for (NSDictionary *moderatorsDis in [block objectForKey:@"moderators"]) {
            _titltModel = [CCForumButtonModel modelWithDictionary:moderatorsDis];
            
            [_titleArary addObject:_titltModel];
            
        }
        NSString *str = [NSString stringWithFormat:@"%@",[block objectForKey:@"themeNum"]];
        NSString *postStr = [NSString stringWithFormat:@"%@",[block objectForKey:@"postNum"]];
        [_titleArary addObject:str];
        [_titleArary addObject:postStr];
        [self.forumPic setDataModel:self.titleArary];
        [self.collectionPushTableView reloadData];
    }];
    
    
    NSString *addStr = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getPostListNew.php?forumId=%@&limit=20&offset=%ld&selectId=0&sortType=1&type=0&uid=&ver=6.2.2",self.forumId,number * 20];
    [AFNblock getdataWithString:addStr block:^(id block) {
        if (self.isLoding == NO) {
            [_pushArray removeAllObjects];
        }
        for (NSDictionary *postListDic in [block objectForKey:@"postList"]) {
            
            _pushModel = [CCForumPushModel modelWithDictionary:postListDic];
            [_pushArray addObject:_pushModel];
        }
        [self.collectionPushTableView.footer endRefreshing];
        [self.collectionPushTableView.header endRefreshing];
        [_collectionPushTableView reloadData];
        
    }];
    
    
}

#pragma mark - 创建tableview
-(void)creatTableViewVC
{
    self.forumPic = [[CCForumPicture alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 100)];
    self.forumPic.bigTitle.text = self.forumName;
    [self.view addSubview:self.forumPic];
    self.collectionPushTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, mainScreen_Width, mainScree_Height - 64)];
    [self.view addSubview:self.collectionPushTableView];
    self.collectionPushTableView.delegate = self;
    self.collectionPushTableView.dataSource = self;
    self.collectionPushTableView.tableHeaderView = self.forumPic;
    
}
#pragma mark - tableview & delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _pushArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return mainScree_Height / 7.411;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    CCForumCollectionPushTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CCForumCollectionPushTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    _pushModel  = [self.pushArray objectAtIndex:indexPath.row];
    [cell setData:_pushModel];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.picView.hidden = YES;
    if (cell.isType == YES) {
        cell.picView.hidden = NO;
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CCFormWebViewController *webVC = [[CCFormWebViewController alloc]init];
    _pushModel= [_pushArray objectAtIndex:indexPath.row];
    webVC.webStr = _collectionModel.postLink;
    [self.navigationController pushViewController:webVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
